/*LICENSE_BEGIN
 * ******************************************************************
 * All original parts of SoccerScope2003 have copyright (2003) by
 * Team YowAI, represented by Mr. Shuhei Shiota
 * The Department of Creative Informatics
 * The Graduate School of Information Science and Technology
 * The University of Tokyo
 * 
 * Additional classes (except libraries in /lib) were developed by 
 * the Dainamite Robocup-Team of the DAI-Labor, TU-Berlin 
 * (www.dai-labor.de)
 * 
 * Extended/new classes/packages are:
 *   soccerscope.analyzer.LogfileAnalyzer
 *   soccerscope.daistatistic.*
 *   soccerscope.daistatistic.charts.*
 *   soccerscope.daistatistic.tools.*
 *   soccerscope.db.*
 *   soccerscope.dialog.*
 *   soccerscope.model.PlayerWorldModel
 *   soccerscope.model.SimplePlayer
 *   soccerscope.net.AgentConnector
 *   soccerscope.training.*
 *   soccerscope.training.conditions.*
 * ******************************************************************
LICENSE_END*/
package soccerscope.daistatistic;

import java.awt.BorderLayout;
import java.awt.Button;
import java.awt.Color;
import java.awt.Dialog;
import java.awt.Dimension;
import java.awt.GraphicsEnvironment;
import java.awt.GridLayout;
import java.awt.Panel;
import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;

/**
 * 
 * this class defines the Action Dialog and provides getter methods for the
 * dialog textfields
 */
public class ActionDialog implements WindowListener {

	private static int dialogWidth = 330; // dimensions of the dialog

	private static int dialogHigh = 8 * 24;

	public static int actualPlayerNumber = 1; // show the player with that

	// number

	public static Dialog dialog; // reference to a Dialog

	private static Button dash; // reference buttons and textfields

	private static TextField dashText;

	private static Button move;

	private static TextField moveText1;

	private static TextField moveText2;

	private static Button kick;

	private static TextField kickText1;

	private static TextField kickText2;

	private static Button say;

	private static TextField sayText;

	private static Button searchBall;

	private static Button turn;

	private static TextField turnText;

	private static Button runToBall;

	private static Button turnNeck;

	private static TextField turnNeckText;

	private static Button senseBody;

	private static Button catchBall;

	private static TextField catchBallText;

	private static Button viewMode;

	private static TextField viewModeText1;

	private static TextField viewModeText2;

	private ActionDialogListener listener = new ActionDialogListener();// create

	// an
	// action
	// listener

	/**
	 * 
	 * the constructor for the ActionDialog
	 * 
	 * @param SoccerScopeMenuBar
	 * @param SoccerScope
	 */
	public ActionDialog(soccerscope.SoccerScope frame) {

		GraphicsEnvironment ge = GraphicsEnvironment
				.getLocalGraphicsEnvironment();

		dialog = new Dialog(frame, "Player actions"); // create a new dialog
		// with our scope as
		// "parent"
		dialog.setVisible(false);
		dialog.setSize(dialogWidth, dialogHigh); // set size

		int x = frame.getX() + frame.getWidth(); // get dimensions of the
		// main window
		while (x > (ge.getMaximumWindowBounds().width - dialogWidth)) { // and
			// put
			// the
			// dialog
			// to
			// the
			// top
			// left
			// side
			x--;
		}

		dialog.setLocation(x, frame.getY());
		dialog.addWindowListener(this); // add a windowListener

		dialog.setLayout(new GridLayout(8, 1, 0, 0));

		// don't show dialog at startup

		// dialog.setResizable(false); //dialog ist not resizeable

		dialog.setBackground(Color.LIGHT_GRAY); // set backgound color

		// define and add all action dialog buttons and textfields

		// dash
		Panel dashPanel = new Panel();
		dashPanel.setLayout(new BorderLayout());
		dash = new Button("dash(power)");
		dash.setPreferredSize(new Dimension(130, 20));
		dash.addActionListener(this.listener);
		dash.setActionCommand("ActionDash");
		dashPanel.add("West", dash);

		dashText = new TextField();
		dashText.setPreferredSize(new Dimension(100, 20));
		dashPanel.add(dashText);
		dashPanel.setPreferredSize(new Dimension(230, 20));
		dialog.add("Center", dashPanel);

		// move
		Panel movePanel = new Panel();
		movePanel.setLayout(new BorderLayout());
		move = new Button("move(x,y)");
		move.setPreferredSize(new Dimension(130, 20));
		move.addActionListener(this.listener);
		move.setActionCommand("ActionMove");
		movePanel.add("West", move);

		moveText1 = new TextField();
		moveText1.setPreferredSize(new Dimension(100, 20));
		movePanel.add("Center", moveText1);

		moveText2 = new TextField();
		moveText2.setPreferredSize(new Dimension(100, 20));
		movePanel.add("East", moveText2);
		movePanel.setPreferredSize(new Dimension(330, 20));

		dialog.add(movePanel);

		// kick
		Panel kickPanel = new Panel();
		kickPanel.setLayout(new BorderLayout());
		kick = new Button("kick(power,direction)");
		kick.setPreferredSize(new Dimension(130, 20));
		kick.addActionListener(this.listener);
		kick.setActionCommand("ActionKick");
		kickPanel.add("West", kick);

		kickText1 = new TextField();
		kickText1.setPreferredSize(new Dimension(100, 20));

		kickPanel.add("Center", kickText1);

		kickText2 = new TextField();
		kickText2.setPreferredSize(new Dimension(100, 20));
		kickPanel.add("East", kickText2);
		kickPanel.setPreferredSize(new Dimension(330, 20));
		dialog.add(kickPanel);

		// say
		Panel sayPanel = new Panel();
		sayPanel.setLayout(new BorderLayout());
		say = new Button("say(msg)");
		say.setPreferredSize(new Dimension(130, 20));
		say.addActionListener(this.listener);
		say.setActionCommand("ActionSay");
		sayPanel.add("West", say);

		sayText = new TextField();
		sayText.setPreferredSize(new Dimension(100, 20));
		sayPanel.add("Center", sayText);

		searchBall = new Button("Search Ball (off)");
		searchBall.setPreferredSize(new Dimension(100, 20));
		searchBall.addActionListener(this.listener);
		searchBall.setActionCommand("ActionSearchBall");
		sayPanel.setPreferredSize(new Dimension(330, 20));

		sayPanel.add("East", searchBall);
		dialog.add(sayPanel);

		// turn
		Panel turnPanel = new Panel();
		turnPanel.setLayout(new BorderLayout());
		turn = new Button("turn(angle)");
		turn.setPreferredSize(new Dimension(130, 20));
		turn.addActionListener(this.listener);
		turn.setActionCommand("ActionTurn");
		turnPanel.add("West", turn);

		turnText = new TextField();
		turnText.setPreferredSize(new Dimension(100, 20));
		turnPanel.add("Center", turnText);

		runToBall = new Button("Run to Ball (off)");
		runToBall.setPreferredSize(new Dimension(100, 20));
		runToBall.addActionListener(this.listener);
		runToBall.setActionCommand("ActionRunToBall");

		turnPanel.setPreferredSize(new Dimension(330, 20));
		turnPanel.add("East", runToBall);
		dialog.add(turnPanel);
		// turnNeck
		Panel turnNeckPanel = new Panel();
		turnNeckPanel.setLayout(new BorderLayout());
		turnNeck = new Button("turnNeck(angle)");
		turnNeck.setPreferredSize(new Dimension(130, 20));
		turnNeck.addActionListener(this.listener);
		turnNeck.setActionCommand("ActionTurnNeck");
		turnNeckPanel.add("West", turnNeck);

		turnNeckText = new TextField();
		turnNeckText.setPreferredSize(new Dimension(100, 20));
		turnNeckPanel.add("Center", turnNeckText);

		senseBody = new Button("Sense Body");
		senseBody.setPreferredSize(new Dimension(100, 20));
		senseBody.addActionListener(this.listener);
		senseBody.setActionCommand("ActionSenseBody");
		turnNeckPanel.add("East", senseBody);

		turnNeckPanel.setPreferredSize(new Dimension(330, 20));
		dialog.add(turnNeckPanel);
		// catchBall
		Panel catchBallPanel = new Panel();
		catchBallPanel.setLayout(new BorderLayout());
		catchBall = new Button("catchBall(direction)");
		catchBall.setPreferredSize(new Dimension(130, 20));
		catchBall.addActionListener(this.listener);
		catchBall.setActionCommand("ActionCatchBall");
		catchBallPanel.add("West", catchBall);

		catchBallText = new TextField();
		catchBallText.setPreferredSize(new Dimension(100, 20));
		catchBallPanel.add("Center", catchBallText);

		catchBallPanel.setPreferredSize(new Dimension(230, 20));
		dialog.add(catchBallPanel);
		// viewMode
		Panel viewModePanel = new Panel();
		viewModePanel.setLayout(new BorderLayout());
		viewMode = new Button("viewMode(quality, angle)");
		viewMode.setPreferredSize(new Dimension(130, 20));
		viewMode.addActionListener(this.listener);
		viewMode.setActionCommand("ActionViewMode");
		viewModePanel.add("West", viewMode);

		viewModeText1 = new TextField();
		viewModeText1.setPreferredSize(new Dimension(100, 20));
		viewModePanel.add("Center", viewModeText1);

		viewModeText2 = new TextField();
		viewModeText2.setPreferredSize(new Dimension(100, 20));
		viewModePanel.add("East", viewModeText2);

		viewModePanel.setPreferredSize(new Dimension(330, 20));
		dialog.add(viewModePanel);

	}

	/**
	 * 
	 * returns true if the dialog is visible else false
	 * 
	 * @return boolean
	 */
	public boolean dialogVisible() {
		return dialog.isVisible();
	}

	/**
	 * 
	 * set visible or not
	 * 
	 * @param boolean
	 */
	public static void setVisible(boolean mode) {
		dialog.setVisible(mode);
	}

	/**
	 * 
	 * turns the on/off Buttons to off mode, used by PlayerDataDialog after a
	 * new player was selected
	 */
	public static void buttonsOff() {
		searchBall.setLabel("Search Ball (off)");
		runToBall.setLabel("Run to Ball (off)");
		Actions.goToBall = false;
		Actions.searchBallGoesOn = false;
	}

	// getter for all textfields

	/**
	 * 
	 * getter for the Dash TextField
	 * 
	 * @return String the fieldtext
	 */
	public static String getDashText() {
		try {
			Integer.parseInt(dashText.getText());
		} catch (Exception e) {
			return "0";
		}
		return dashText.getText();
	}

	/**
	 * 
	 * getter for the first Move TextField
	 * 
	 * @return String the fieldtext
	 */
	public static String getMoveText1() {
		try {
			Integer.parseInt(moveText1.getText());
		} catch (Exception e) {
			return "0";
		}
		return moveText1.getText();
	}

	/**
	 * 
	 * getter for the second Move TextField
	 * 
	 * @return String the fieldtext
	 */
	public static String getMoveText2() {
		try {
			Integer.parseInt(moveText2.getText());
		} catch (Exception e) {
			return "0";
		}
		return moveText2.getText();
	}

	/**
	 * 
	 * getter for the first Kick TextField
	 * 
	 * @return String the fieldtext
	 */
	public static String getKickText1() {
		try {
			Double.parseDouble(kickText1.getText());
		} catch (Exception e) {
			return "0";
		}
		return kickText1.getText();
	}

	/**
	 * 
	 * getter for the second Kick TextField
	 * 
	 * @return String the fieldtext
	 */
	public static String getKickText2() {
		try {
			Double.parseDouble(kickText2.getText());
		} catch (Exception e) {
			return "0";
		}
		return kickText2.getText();
	}

	/**
	 * 
	 * getter for the Say TextField
	 * 
	 * @return String the fieldtext
	 */
	public static String getSayMsg() {
		return sayText.getText();
	}

	/**
	 * 
	 * getter for the Turn TextField
	 * 
	 * @return String the fieldtext
	 */
	public static String getTurnDegrees() {
		try {
			Double.parseDouble(turnText.getText());
		} catch (Exception e) {
			return "0";
		}

		return turnText.getText();
	}

	/**
	 * 
	 * getter for the TurnNeck TextField
	 * 
	 * @return String the fieldtext
	 */
	public static String getTurnNeckDegrees() {
		try {
			Double.parseDouble(turnNeckText.getText());
		} catch (Exception e) {
			return "0";
		}
		return turnNeckText.getText();
	}

	/**
	 * 
	 * getter for the CatchBall TextField
	 * 
	 * @return String the fieldtext
	 */
	public static String getCatchBallText() {
		try {
			Double.parseDouble(catchBallText.getText());
		} catch (Exception e) {
			return "0";
		}
		return catchBallText.getText();
	}

	/**
	 * 
	 * getter for the first ViewMode TextField
	 * 
	 * @return String the fieldtext
	 */
	public static String getViewModeText1() {
		try {
			Integer.parseInt(viewModeText1.getText());
		} catch (Exception e) {
			return "0";
		}
		return viewModeText1.getText();
	}

	/**
	 * 
	 * getter for the second ViewMode TextField
	 * 
	 * @return String the fieldtext
	 */
	public static String getViewModeText2() {
		try {
			Integer.parseInt(viewModeText2.getText());
		} catch (Exception e) {
			return "0";
		}
		return viewModeText2.getText();
	}

	public static Dialog getDialog() {
		if (dialog == null)
			return null;
		return dialog;
	}

	public static void setTitle(String title) {
		dialog.setTitle(title);
	}

	public void windowOpened(WindowEvent arg0) {
	}

	public void windowClosing(WindowEvent arg0) {
		dialog.setVisible(false);

	}

	public void windowClosed(WindowEvent arg0) {
	}

	public void windowIconified(WindowEvent arg0) {
	}

	public void windowDeiconified(WindowEvent arg0) {
	}

	public void windowActivated(WindowEvent arg0) {
	}

	public void windowDeactivated(WindowEvent arg0) {
	}
}

/**
 * 
 * the ActionDialogListener handles the ActionDialog Actions
 */
class ActionDialogListener implements ActionListener {

	/**
	 * 
	 * if an action performed search the resulting action
	 * 
	 * @param ActionEvent
	 *            the event
	 */
	public void actionPerformed(ActionEvent e) {

		// String s = e.getActionCommand(); //get the ActionCommand
		// Object o = e.getSource(); //get the Action Object

		// set the actual tactic
		// StateTactic actualTactic =
		// Register.getTacticOfPlayer(ActionDialog.actualPlayerNumber);

		// a button reference, used for the on/off buttons
		// Button b;

		// check and perform the Actions

		// if (s.equals("ActionDash")){
		// actualTactic.doAction(new
		// DashAction(Integer.parseInt(ActionDialog.getDashText())));
		// }
		// if (s.equals("ActionMove")){
		// actualTactic.doAction(new
		// MoveAction(Integer.parseInt(ActionDialog.getMoveText1()),Integer.parseInt(ActionDialog.getMoveText2())));
		// }
		// if (s.equals("ActionKick")){
		// actualTactic.doAction(new
		// KickAction(Double.parseDouble(ActionDialog.getKickText1()),Double.parseDouble(ActionDialog.getKickText2())));
		// }
		// if (s.equals("ActionSay")){
		// actualTactic.doAction(new SayAction(ActionDialog.getSayMsg()));
		// }
		// if (s.equals("ActionSearchBall")){
		// b = (Button) o;
		// if (b.getLabel().equals("Search Ball (off)")){
		// b.setLabel("Search Ball (on)");
		// Actions.start(actualTactic,Register.getPlayerBeanWithPlayerNumber(ActionDialog.actualPlayerNumber),Actions.Mode_searchBall);
		// }
		// else{
		// b.setLabel("Search Ball (off)");
		// Actions.start(actualTactic,Register.getPlayerBeanWithPlayerNumber(ActionDialog.actualPlayerNumber),Actions.Mode_searchBall);
		// }
		// }
		// if (s.equals("ActionTurn")){
		// actualTactic.doAction(new
		// TurnAction(Integer.parseInt(ActionDialog.getTurnDegrees())));
		// }
		// if (s.equals("ActionRunToBall")){
		// b = (Button) o;
		// if (b.getLabel().equals("Run to Ball (off)")){
		// b.setLabel("Run to Ball (on)");
		// Actions.start(actualTactic,Register.getPlayerBeanWithPlayerNumber(ActionDialog.actualPlayerNumber),Actions.Mode_goToBall);
		// }
		// else{
		// b.setLabel("Run to Ball (off)");
		// Actions.start(actualTactic,Register.getPlayerBeanWithPlayerNumber(ActionDialog.actualPlayerNumber),Actions.Mode_goToBall);
		// }
		// }
		// if (s.equals("ActionTurnNeck")){
		// actualTactic.doAction(new
		// TurnNeckAction(Integer.parseInt(ActionDialog.getTurnNeckDegrees())));
		// }
		// if (s.equals("ActionSenseBody")){
		// actualTactic.doAction(new SenseBodyAction());
		// }
		// if (s.equals("ActionCatchBall")){
		// actualTactic.doAction(new
		// CatchAction(Integer.parseInt(ActionDialog.getCatchBallText())));
		// }
		// if (s.equals("ActionViewMode")){
		// actualTactic.doAction(new
		// ChangeViewModeAction(Integer.parseInt(ActionDialog.getViewModeText1()),Integer.parseInt(ActionDialog.getViewModeText2())));
		// }

	}

}